import 'package:flutter/material.dart';
import 'package:x3_charts_core/model/single_histogram_chart_widget_model.dart';

///**
///* @author hrongyong
///* @date   2/24/21
///* @email hrongyong@gmail.com
///* Description:
///*/

class SingleHistogramChartPainter extends CustomPainter {
  ///图表数组
  final List<SingleHistogramChartWidgetModel> chartList;

  ///Y轴最大值
  final int yAxisMax;

  ///背景颜色
  final Color backgroundColor;

  ///x轴线颜色
  final Color xAxisColor;

  ///x轴线描述颜色
  final Color xAxisDescriptionColor;

  ///y轴线颜色
  final Color yAxisColor;

  ///y轴线描述颜色
  final Color yAxisDescriptionColor;

  ///绘制X轴线
  final bool drawXAxis;

  ///绘制X轴线刻度
  final bool drawXAxisScale;

  ///绘制Y轴线
  final bool drawYAxis;

  ///绘制Y轴线刻度
  final bool drawYAxisScale;

  ///直方图填充颜色
  final Color histogramFillColor;

  ///动画数值
  final Animation<double> repaint;

  SingleHistogramChartPainter({
    @required this.chartList,
    @required this.yAxisMax,
    @required this.repaint,
    @required this.histogramFillColor,
    this.backgroundColor = Colors.white,
    this.xAxisColor = Colors.black12,
    this.yAxisColor = Colors.black12,
    this.xAxisDescriptionColor = Colors.black,
    this.yAxisDescriptionColor = Colors.black,
    this.drawXAxis = true,
    this.drawXAxisScale = true,
    this.drawYAxis = false,
    this.drawYAxisScale = true,
  }) : super(repaint: repaint);

  double maxWidth = 0.0;
  double maxHeight = 0.0;
  double xStep = 0.0; // x 间隔
  double yStep = 0.0; // y 间隔

  ///左边间距
  double leftMargin = 0.0;

  ///底部间距
  double bottomMargin = 0.0;

  ///间距
  EdgeInsets chartPadding;

  ///轴线画笔
  Paint axisPaint = Paint()..strokeWidth = 1.0;

  //直方图的填充颜色
  Paint fillPaint = Paint()..color = Colors.red;

  ///文字
  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);

  @override
  void paint(Canvas canvas, Size size) {
    if (chartList.length == 0) return;

    ///确定左边和下边的距离
    TextPainter painter = TextPainter(
        maxLines: 1,
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: yAxisMax.toString(),
            style: TextStyle(
              fontSize: 10,
            )));
    painter.layout();
    leftMargin = painter.width;
    bottomMargin = painter.height;

    ///确定左边间距
    chartPadding = EdgeInsets.only(left: leftMargin + 2, bottom: bottomMargin + 4, right: 10, top: 10);

    ///可绘画的宽度
    maxWidth = size.width - chartPadding.left - chartPadding.right;

    /// x 间隔
    xStep = maxWidth / chartList.length;

    ///可绘画的高度
    maxHeight = size.height - chartPadding.top - chartPadding.bottom;

    /// y 间隔
    yStep = maxHeight / 10;

    ///绘制背景
    canvas.drawRect(Offset.zero & size, Paint()..color = backgroundColor);

    ///移动绘画中心
    canvas.translate(chartPadding.left, size.height - chartPadding.bottom);

    ///绘制X轴刻度和文本
    drawXAxisScaleAndText(canvas, size);

    ///绘制Y轴刻度和文本
    drawYAxisScaleAndText(canvas, size);

    ///绘制直方图和描述
    drawHistogramAndDescription(canvas, size);
  }

  drawHistogramAndDescription(Canvas canvas, Size size) {
    fillPaint..color = histogramFillColor;
    canvas.save();
    canvas.translate(xStep / 2, 0);
    for (int i = 0; i < chartList.length; i++) {
      double height = -chartList[i].histogramScale * maxHeight;
      Rect rect = Rect.fromLTWH(0, 0, xStep / 2, height * repaint.value).translate(-xStep / 4, 0);

      ///绘制直方
      canvas.drawRect(rect, fillPaint);

      ///绘制文本
      _drawHistogramDescriptionText(
        canvas,
        title: chartList[i].description,
        offset: Offset(0, height * repaint.value),
      );
      canvas.translate(xStep, 0);
    }
    canvas.restore();
  }

  ///绘制Y轴文字
  void _drawHistogramDescriptionText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    double fontSize = 6,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx - textSize.width / 2, offset.dy - textSize.height - 2);
    _textPainter.paint(canvas, textOffsetPos);
  }

  drawYAxisScaleAndText(Canvas canvas, Size size) {
    canvas.save();

    ///Y轴线
    if (drawYAxis) canvas.drawLine(Offset.zero, Offset(0, -maxHeight), axisPaint..color = yAxisColor);
    if (drawYAxisScale) {
      for (int i = 0; i <= 10; i++) {
        canvas.drawLine(Offset.zero, Offset(-4, 0), axisPaint..color = xAxisColor);
        _drawYAxisText(canvas, title: (yAxisMax / 10 * i).toInt().toString(), offset: Offset.zero);
        canvas.translate(0, -yStep);
      }
    }
    canvas.restore();
  }

  ///绘制Y轴文字
  void _drawYAxisText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    double fontSize = 6,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx - textSize.width - 4, offset.dy - textSize.height / 2);
    _textPainter.paint(canvas, textOffsetPos);
  }

  drawXAxisScaleAndText(Canvas canvas, Size size) {
    canvas.save();

    ///x轴线
    if (drawXAxis) canvas.drawLine(Offset.zero, Offset(maxWidth, 0), axisPaint..color = xAxisColor);
    canvas.translate(xStep, 0);

    ///x轴刻度线
    if (drawXAxisScale) {
      for (int i = 0; i < chartList.length; i++) {
        canvas.drawLine(Offset(-xStep / 2, 0), Offset(-xStep / 2, 4), axisPaint..color = xAxisColor);

        ///x轴线描述
        _drawXAxisText(canvas, title: chartList[i].name, offset: Offset(-xStep / 2, 0));
        canvas.translate(xStep, 0);
      }
    }
    canvas.restore();
  }

  ///绘制X轴文字
  void _drawXAxisText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    double fontSize = 6,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx - textSize.width / 2, offset.dy + textSize.height / 3);
    _textPainter.paint(canvas, textOffsetPos);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
